package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.entity.Food;
import com.neuedu.yyzx.framework.Res;
import com.neuedu.yyzx.service.IFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 膳食信息 前端控制器
 * </p>
 *
 * @author Kobin
 * @since 2024-04-24
 */
@RestController
@RequestMapping("/food")
public class FoodController {
    @Autowired
    IFoodService foodService;
    /**
     * http://localhost:8080/food/list
     * @param food
     * @return
     */
    @RequestMapping("/list")
    public Res<List<Food>> list(Food food) {

        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        return Res.ok(foodService.list(queryWrapper));
    }
    //分页查询
    @RequestMapping("/page")
    public Res<Page> page(Page<Food> page, Food food) {
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        if(food.getFoodname()!= null && !"".equals(food.getFoodname().trim())){
            queryWrapper.like("foodname" ,food.getFoodname() );
        }
        return Res.ok(foodService.page(page,queryWrapper));
    }



    @RequestMapping("/saveOrUpdate")
    public Res  saveOrUpdate( @RequestBody Food food){
        return  Res.ok(foodService.saveOrUpdate(food));
    }


    @RequestMapping("/removeById/{id}")
    public Res  removeById(@PathVariable int id){
        return  Res.ok(foodService.removeById(id));
    }


    @RequestMapping("/getById/{id}")
    public Res<Food> getById(@PathVariable  int id){
        return  Res.ok(foodService.getById(id));
    }
}
